using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
namespace DAL
{
    public class Photos : DAL.DataManager<Photo>
    {
        private static Photos Instance = new Photos();
        static Photos() { }

        public override string TableName
        {
            get { return @"awesome_pictures"; }
        }

        protected override void CopyToItem(DataRow Row, Photo Item)
        {
            Item.User = Users.Item(Convert.ToInt64(Row["UserID"]));

            Item.SHA512 = Convert.ToString(Row["SHA512"]);

            Item.Rating = Convert.ToInt64(Row["Rating"]);

            Item.DateUploaded = Convert.ToDateTime(Row["DateUploaded"]);

            Item.Visible = Convert.ToBoolean(Row["Visible"]);

            Item.FileSize = Convert.ToInt64(Row["FileSize"]);

            Item.FileExtension = (Photo.Extensions)Convert.ToInt32(Row["FileExtension"]);

            Item.Size = new System.Drawing.Size(Convert.ToInt32(Row["Width"]), Convert.ToInt32(Row["Height"]));
        }

        protected override void CopyFromItem(DataManagerQuery query, Photo Item)
        {
            query.AddParam("UserID", Item.User.ID);

            query.AddParam("SHA512", Item.SHA512);

            query.AddParam("Rating", Item.Rating);

            query.AddParam("DateUploaded", Item.DateUploaded);

            query.AddParam("Visible", Item.Visible);

            query.AddParam("FileSize", Item.FileSize);

            query.AddParam("FileExtension", (int)Item.FileExtension);

            query.AddParam("Width", Item.Size.Width);

            query.AddParam("Height", Item.Size.Height);
        }

        public static Photo Item(Int64 id)
        {
            return Instance.GetItem(id);
        }

        public static List<Photo> All()
        {
            return Instance.GetItems();
        }

        public static void Add(Photo Item)
        {
            Instance.Insert(Item);
        }

        public static void Save(Photo Item)
        {
            Instance.Update(Item);
        }

        public static List<Photo> GetByAlbum(Album album)
        {
            DataManagerQuery query = new DataManagerQuery();
            query.AddParam("AlbumID", album.ID);
            return Instance.GetItems(query);
        }

        public static Photo GetByHash(string hash)
        {
            DataManagerQuery query = new DataManagerQuery();
            query.AddParam("SHA512", hash);

            List<Photo> items = Instance.GetItems(query);
            if (items.Count > 0)
            {
                return items[0];
            }
            else
            {
                return null;
            }
        }
    }

    public class Photo : DAL.DataManagerRow
    {
        public enum Extensions
        {
            JPG,
            GIF,
            BMP,
            PNG
        }

        public static Extensions GetExtension(string ext)
        {
            ext = ext.ToLower();
            switch (ext)
            {
                case "jpg":
                case "jpeg":
                    return Extensions.JPG;
                    break;
                case "gif":
                    return Extensions.GIF;
                    break;
                case "bmp":
                    return Extensions.BMP;
                    break;
                default:
                    return Extensions.PNG;
            }
        }

        public static string ComputeHash(byte[] bytes)
        {
            System.Security.Cryptography.SHA512Managed sha512 = new System.Security.Cryptography.SHA512Managed();
            byte[] hash = sha512.ComputeHash(bytes);
            return BitConverter.ToString(hash).Replace("-", "").ToLower();
        }

        private User _User;
        public User User
        {
            get
            {
                return _User;
            }
            set
            {
                _User = value;
            }
        }

        private string _SHA512;
        public string SHA512
        {
            get
            {
                return _SHA512;
            }
            set
            {
                _SHA512 = value;
            }
        }

        private Int64 _Rating;
        public Int64 Rating
        {
            get
            {
                return _Rating;
            }
            set
            {
                _Rating = value;
            }
        }

        private DateTime _DateUploaded;
        public DateTime DateUploaded
        {
            get
            {
                return _DateUploaded;
            }
            set
            {
                _DateUploaded = value;
            }
        }

        private bool _Visible;
        public bool Visible
        {
            get
            {
                return _Visible;
            }
            set
            {
                _Visible = value;
            }
        }

        private long _filesize;
        public long FileSize
        {
            get
            {
                return _filesize;
            }
            set
            {
                _filesize = value;
            }
        }

        private Extensions _ext;
        public Extensions FileExtension
        {
            get
            {
                return _ext;
            }
            set
            {
                _ext = value;
            }
        }

        public string FileName
        {
            get
            {
                return string.Format("{0}.{1}", ID, FileExtension);
            }
        }

        private System.Drawing.Size _Size;
        public System.Drawing.Size Size
        {
            get
            {
                return _Size;
            }
            set
            {
                _Size = value;
            }
        }

        public string ThumbFileName
        {
            get
            {
                return string.Format("{0}_small.png", ID);
            }
        }
    }
}